package net.adetech.dcweb.core.ddpush.utils.impl;

import com.alibaba.fastjson.JSONObject;
import net.adetech.dcweb.core.ddpush.utils.IDDPushService;
import org.ddpush.im.util.StringUtil;
import org.ddpush.im.v1.client.appserver.Pusher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Iterator;

@Service
public class DDPushServiceImpl implements IDDPushService{

    @Value("${ddpush.ip}")
    private String ddpushServerIp;
    @Value("${ddpush.port}")
    private int ddpushServerPort;
    @Override
    public void push(String userId, String title, String content,JSONObject info) {
        try {

            Long notifyId = (long)(Math.random()*1000000000);
            byte[] uuid = StringUtil.md5Byte(userId);
            Pusher pusher = new Pusher(ddpushServerIp,ddpushServerPort,20000,1,1);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title",title);
            jsonObject.put("content",content);
            jsonObject.put("notifyId",notifyId);
            Iterator<String> it = info.keySet().iterator();
            while (it.hasNext())
            {
                String key = it.next();
                Object value = info.get(key);
                jsonObject.put(key,value);
            }
            pusher.push0x20Message(uuid,jsonObject.toJSONString().getBytes("UTF-8"));
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
